package com.backedserver.controller.user;

import com.backedserver.base.CaffeineUtils;
import com.common.global.BizException;
import com.common.global.CommonEnum;
import com.common.properties.JwtProperties;
import com.common.requestvo.system.UserReqVo;
import com.common.service.system.UserService;
import com.common.utils.JwtTokenUtils;
import com.common.utils.PageUtil;
import com.common.utils.ResultVoUtil;
import com.common.utils.TokenUtils;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.system.UserResultVo;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户 Controller
 *
 * @author johnny
 * @create 2020-07-10 下午4:08
 **/
@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "用户管理相关的接口")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/logout")
    @ApiOperation(value = "退出接口")
    public ResultVo logout(HttpServletRequest request) {
        String token = TokenUtils.getTokenFromRequest(request);
        if (StringUtils.isNotEmpty(token)) {
            Long userId;
            try {
                userId = JwtTokenUtils.getUserIdFromToken(token);
                String username = JwtTokenUtils.getUserNameFromToken(token);
                CaffeineUtils.getTokenCache().asMap().remove(userId + "_" + username);
            } catch (Exception e) {
                //throw new BizException(CommonEnum.REFRESH_TOKEN_EXPIRED);
                return ResultVoUtil.success();
            }
        }
        return ResultVoUtil.success();
    }

//    @GetMapping("/userInfo")
//    public ResultVo userInfo(HttpServletRequest request) {
//        UserResultVo userResultVo = null;
//        String token = TokenUtils.getTokenFromRequest(request);
//        if (StringUtils.isNotEmpty(token)) {
//            Long userId;
//            try {
//                userId = JwtTokenUtils.getUserIdFromToken(token);
//            } catch (ExpiredJwtException e) {
//                throw new BizException(CommonEnum.TOKEN_ERROR);
//            }
//            if (userId != 0L) {
//                userResultVo = userService.findUserInfo(userId);
//                log.info("【userInfo userResultVo ： {}】", userResultVo);
//            }
//        }
//        return ResultVoUtil.success(userResultVo);
//    }

    @GetMapping("/listByCondition")
    public ResultVo listByCondition(UserReqVo userReqVo, PageVo pageVo) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        Page<UserResultVo> page = userService.listByCondition(userReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }

    @PostMapping("/submitUser")
    public ResultVo submitUser(@RequestBody UserReqVo userReqVo) {
        log.info("【submitUser userReqVo ： {}】  ", userReqVo);
        userService.submitUser(userReqVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/edit")
    public ResultVo edit(@RequestBody UserReqVo userReqVo) {
        log.info("【edit userReqVo ： {}】  ", userReqVo);
        if (userReqVo.getId() == null) {
            return ResultVoUtil.error("id can not null");
        }
        userService.edit(userReqVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/delete/{id}")
    public ResultVo delete(@PathVariable("id") Long userId) {
        log.info("【delete userId ： {}】  ", userId);
        userService.deleteById(userId);
        return ResultVoUtil.success();
    }


}